package com.ruoyi.system.service.impl;

import java.sql.Array;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AdviceMapper;
import com.ruoyi.system.domain.Advice;
import com.ruoyi.system.service.IAdviceService;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-03
 */
@Service
public class AdviceServiceImpl implements IAdviceService 
{
    @Resource
    private AdviceMapper adviceMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param adviceId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Advice selectAdviceByAdviceId(Long adviceId)
    {
        return adviceMapper.selectAdviceByAdviceId(adviceId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param advice 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Advice> selectAdviceList(Advice advice)
    {
        return adviceMapper.selectAdviceList(advice);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param advice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAdvice(Advice advice)
    {
        advice.setFakeDelete(1L);
        advice.setUserId(101l);
        advice.setAdviceTime(DateUtils.getNowDate());
        return adviceMapper.insertAdvice(advice);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param advice 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdvice(Advice advice)
    {
        advice.setReplyTime(DateUtils.getNowDate());
        advice.setUserId(SecurityUtils.getLoginUser().getUserId());
        return adviceMapper.updateAdvice(advice);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param adviceIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdviceByAdviceIds(Long[] adviceIds)
    {
        String[] str = new String[adviceIds.length];
        for (int i = 0; i < adviceIds.length; i++) {
            Advice advice = adviceMapper.selectAdviceByAdviceId(adviceIds[i]);
            if (advice.getUserId() == 101) {
                str[i] = "0";
            }else {
                str[i]="1";
            }
        }
        String toString = Arrays.toString(str);
        if (toString.contains("0")){
            return 101;
        }else {
            return adviceMapper.deleteAdviceByAdviceIds(adviceIds);
        }
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param adviceId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdviceByAdviceId(Long adviceId)
    {
        return adviceMapper.deleteAdviceByAdviceId(adviceId);
    }

    @Override
    public List<Advice> adviceList() {
        Advice advice=new Advice();
        advice.setUserId(SecurityUtils.getLoginUser().getUserId());
        return adviceMapper.adviceList(advice);
    }

    @Override
    public List<Advice> noReplyAdvice() {
        return adviceMapper.noReplyAdvice();
    }
}
